<?php
declare (strict_types=1);

namespace app\command;

use app\data\model\CaseCategoryModel;
use app\data\model\CaseModel;
use app\data\model\CmsCatModel;
use app\data\model\CmsModel;
use app\model\Blog;
use app\model\BlogCat;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class ImportCases extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('import_cases')
            ->setDescription('导入案例');
    }

    protected function execute(Input $input, Output $output)
    {
        $caseModel = new CaseModel();
        $caseCatModel = new CaseCategoryModel();
        $blogModel = new Blog();
        $blogCatModel = new BlogCat();
        // 清空数据
        $caseModel->where(true)->delete();
        $caseCatModel->where(true)->delete();
        $output->writeln('数据已清空');

        $root_cat_id = 215;
        $cat_ids = [];
        $blogCatModel->where('parent_id', $root_cat_id)->chunk(10, function ($cats) use (
            $caseCatModel, $output, &$cat_ids
        ) {
            $insertData = [];
            foreach ($cats as $cat) {
                $rewrite_url = $cat['urlrewriter'] ? 'article/' . $cat['urlrewriter'] : 'article/' . $cat['cat_id'];
                $insertData[] = [
                    "id" => $cat['cat_id'],
                    "name" => $cat['cat_name'],
                    "sort" => $cat['sort_order'],
                    "rewrite_url" => $rewrite_url,
                    "pic" => $cat['pic'],
                    'ver' => 0,
                    'create_time' => time(),
                    'update_time' => time(),
                    'delete_time' => 0,
                    'is_show' => 1,
                    'content' => $cat['ms']
                ];
                $cat_ids[] = $cat['cat_id'];

                updateRoute(
                    $cat['cat_name'],
                    '/index/page/cases',
                    strval($rewrite_url),
                    strval($rewrite_url),
                    ['id' => $cat['cat_id']],
                    'cases'
                );
            }
            $result = $caseCatModel->insertAll($insertData);
            $output->writeln('案例分类导入成功,数量：' . $result);

        });

        $blogModel->whereIn('cat_id', $cat_ids)->chunk(10, function ($blogs) use (
            $caseModel, $output
        ) {
            $insertData = [];
            foreach ($blogs as $blog) {
                $rewrite_url = $blog['urlrewriter'] ? 'articledetail/' . $blog['urlrewriter'] : 'articledetail/' . $blog['article_id'];
                $insertData[] = [
                    'id' => $blog['article_id'],
                    'cat_id' => $blog['cat_id'],
                    'name' => $blog['title'],
                    'desc' => $blog['des'],
                    'pic' => $blog['photo'],
                    'content' => $blog['content'],
                    'sort' => $blog['sort_order'],
                    'is_show' => $blog['is_show'],
                    "ver" => 0,
                    "create_time" => strtotime($blog['showtime']) ?? time(),
                    "update_time" => time(),
                    "delete_time" => 0,
                    'rewrite_url' => $rewrite_url,
                    'icon' => $blog['pt1'],
                    'ext_pic' => $blog['pt2'],
                    'download_file' => $blog['down_file'],
                ];
                updateRoute(
                    $blog['article_id'],
                    '/index/page/casesinfo',
                    strval($rewrite_url),
                    strval($rewrite_url),
                    ['id' => $blog['article_id']],
                    'cases'
                );
            }
            $result = $caseModel->insertAll($insertData);
            $output->writeln('案例导入成功,数量：' . $result);

        });

        $output->writeln('结束案例导入');
    }
}
